/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.domain;

import han.bifang.gwt.client.common.ContentChangeListener;
import han.bifang.gwt.client.common.EntityConstants;
import han.bifang.gwt.client.common.GwtContext;
import han.bifang.gwt.client.common.TreeNodeChangeListener;
import han.bifang.gwt.client.common.TreeNodeContentListener;
import han.bifang.gwt.client.common.entity.Domain;
import han.bifang.gwt.client.common.entity.ServiceBus;
import han.bifang.gwt.client.common.entity.ServiceCatalog;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;
import han.bifang.gwt.client.domain.content.BusDetailContent;
import han.bifang.gwt.client.domain.content.CatalogDetailContent;
import han.bifang.gwt.client.domain.content.CreateOrModifyBusContent1;
import han.bifang.gwt.client.domain.content.CreateOrModifyBusContent2;
import han.bifang.gwt.client.domain.content.CreateOrModifyCatalogContent1;
import han.bifang.gwt.client.domain.content.CreateOrModifyCatalogContent2;
import han.bifang.gwt.client.domain.content.CreateOrModifyOrRemoveDomainContent;
import han.bifang.gwt.client.domain.content.DomainDetailContent;
import han.bifang.gwt.client.domain.content.DomainRootContent;
import han.bifang.gwt.client.domain.content.RemoveBusContent;
import han.bifang.gwt.client.domain.content.RemoveCatalogContent;
import han.bifang.gwt.client.domain.tree.DomainTree;
import han.bifang.gwt.client.domain.tree.DomainTreeItem;

import java.util.HashMap;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TreeItem;

public class DomainTab extends Composite implements TreeNodeContentListener,
		ContentChangeListener {

	private TreeItem selectedItem = null;

	private DecoratorPanel panel = null;

	private DomainMessages constants = null;

	private BifangGWTServiceAsync service = null;

	private TreeNodeChangeListener listener = null;
	
	private GwtContext context = null;

	public void setTreeNodeChangeListener(TreeNodeChangeListener l) {

		this.listener = l;
	}

	public DomainTab(BifangGWTServiceAsync service, GwtContext context) {
		super();
		this.context = context;
		
		constants = GWT.create(DomainMessages.class);

		this.service = service;
		

		HorizontalPanel hp = new HorizontalPanel();
		hp.setWidth("100%");

		DecoratorPanel treePanel = new DecoratorPanel();
		treePanel.setWidth("100%");

		DomainTree tree = new DomainTree(constants, service);
		tree.setTreeNodeContentListener(this);
		this.setTreeNodeChangeListener(tree);

		treePanel.add(tree);
		hp.add(treePanel);

		panel = new DecoratorPanel();
		panel.setWidth("100%");
		hp.add(panel);
		hp.setCellWidth(treePanel, "30%");
		hp.setCellWidth(panel, "70%");
		// hp.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		initWidget(hp);

	}

	public void onLoad() {
		DomainRootContent drc = new DomainRootContent(constants, service,context);
		drc.setContentChangeListener(this);
		panel.setWidget(drc);

	}

	public void onTreeNodeSelected(TreeItem item) {
		this.selectedItem = item;
		DomainTreeItem di = (DomainTreeItem) item;
		if (di.getSubType().equals(DomainConstants.DOMAIN_ROOT)) {
			DomainRootContent drc = new DomainRootContent(constants, service,context);
			drc.setContentChangeListener(this);
			panel.setWidget(drc);
		} else if (di.getSubType().equals(EntityConstants.TREE_DOMAIN_LOCAL)
				|| di.getSubType().equals(EntityConstants.TREE_DOMAIN_REMOTE)) {
			DomainDetailContent ddc = new DomainDetailContent(constants,
					service, di.getKey(),context);
			ddc.setContentChangeListener(this);
			panel.setWidget(ddc);

		} else if (di.getSubType().equals(EntityConstants.TREE_DOMAIN_CATALOG)) {
			CatalogDetailContent ddc = new CatalogDetailContent(constants,
					service, di.getKey(),context);
			ddc.setContentChangeListener(this);
			panel.setWidget(ddc);

		}else if (di.getSubType().equals(EntityConstants.TREE_DOMAIN_BUS)) {
			BusDetailContent ddc = new BusDetailContent(constants,
					service, di.getKey(),context);
			ddc.setContentChangeListener(this);
			panel.setWidget(ddc);

		}

	}

	public void onContentChanged(String source, String dest, HashMap props) {
		if (dest.equals(DomainContentConstants.CREATE_DOMAIN)) {
			boolean isLocal = (Boolean) props.get("local");
			// Window.alert("" + isLocal);
			CreateOrModifyOrRemoveDomainContent c = new CreateOrModifyOrRemoveDomainContent(
					this.constants, this.service, isLocal,
					CreateOrModifyOrRemoveDomainContent.CREATE, null);
			c.setContentChangeListener(this);
			this.panel.setWidget(c);
			return;
		}
		if (source.equals(DomainContentConstants.CREATE_DOMAIN)
				&& dest.equals(DomainContentConstants.DOMAIN_ROOT)) {
			Domain d = (Domain) props.get("domain");
			DomainTreeItem t = new DomainTreeItem();
			t.setId(d.getUnitId());
			t.setHTML(d.getName());
			t.setKey(d.getId());
			if (d.getLocal()) {
				t.setSubType(EntityConstants.TREE_DOMAIN_LOCAL);
			} else {
				t.setSubType(EntityConstants.TREE_DOMAIN_REMOTE);
			}
			t.setType(EntityConstants.TREE_DOMAIN);
			this.listener.onAddNode(t);

			DomainRootContent drc = new DomainRootContent(constants, service,context);
			drc.setContentChangeListener(this);
			panel.setWidget(drc);
			return;

		}
		if (source.equals(DomainContentConstants.DOMAIN_DETAIL)
				&& dest.equals(DomainContentConstants.MODIFY_DOMAIN)) {
			Domain d = (Domain) props.get("domain");
			CreateOrModifyOrRemoveDomainContent c = new CreateOrModifyOrRemoveDomainContent(
					this.constants, this.service, d.getLocal(),
					CreateOrModifyOrRemoveDomainContent.MODIFY, d);
			c.setContentChangeListener(this);
			this.panel.setWidget(c);
			return;
		}
		if (source.equals(DomainContentConstants.MODIFY_DOMAIN)
				&& dest.equals(DomainContentConstants.DOMAIN_DETAIL)) {
			Domain d = (Domain) props.get("domain");
			selectedItem.setHTML(d.getName());
			// listener.onModifyNode(selectedItem);
			DomainDetailContent ddc = new DomainDetailContent(constants,
					service, d.getId(),context);
			ddc.setContentChangeListener(this);
			panel.setWidget(ddc);
			return;
		}
		if (source.equals(DomainContentConstants.REMOVE_DOMAIN)
				&& dest.equals(DomainContentConstants.DOMAIN_ROOT)) {
			listener.onRemoveNode(selectedItem);
			DomainRootContent ddc = new DomainRootContent(constants, service,context);
			ddc.setContentChangeListener(this);
			panel.setWidget(ddc);
			return;
		}
		if (source.equals(DomainContentConstants.REMOVE_DOMAIN)
				&& dest.equals(DomainContentConstants.DOMAIN_DETAIL)) {
			DomainTreeItem i = (DomainTreeItem) selectedItem;
			DomainDetailContent ddc = new DomainDetailContent(constants, service,i.getKey(),context);
			ddc.setContentChangeListener(this);
			panel.setWidget(ddc);
			return;
		}
		if (source.equals(DomainContentConstants.DOMAIN_DETAIL)
				&& dest.equals(DomainContentConstants.REMOVE_DOMAIN)) {

			Domain d = (Domain) props.get("domain");
			CreateOrModifyOrRemoveDomainContent rc = new CreateOrModifyOrRemoveDomainContent(
					constants, service, d.getLocal(),
					CreateOrModifyOrRemoveDomainContent.REMOVE, d);
			rc.setContentChangeListener(this);
			panel.setWidget(rc);
			return;
		}

		if (source.equals(DomainContentConstants.DOMAIN_DETAIL)
				&& dest.equals(DomainContentConstants.CREATE_CAT1)) {

			String dId = (String) props.get("domainId");
			CreateOrModifyCatalogContent1 cc = new CreateOrModifyCatalogContent1(
					constants, dId, true, null, null);
			cc.setContentChangeListener(this);
			panel.setWidget(cc);
			return;
		}
		if (source.equals(DomainContentConstants.CREATE_CAT1)
				&& dest.equals(DomainContentConstants.CREATE_CAT2)) {
			DomainTreeItem i = (DomainTreeItem) selectedItem;
			//String url = (String) props.get("url");
			//ServiceCatalog sc = (ServiceCatalog) props.get("cat");
			props.put("parentTreeId", i.getId());
			props.put("create", true);
			CreateOrModifyCatalogContent2 cc = new CreateOrModifyCatalogContent2(
					constants, service,props);
			cc.setContentChangeListener(this);
			panel.setWidget(cc);
			return;
		}
		if (source.equals(DomainContentConstants.CREATE_CAT2)
				&& dest.equals(DomainContentConstants.DOMAIN_DETAIL)) {
			DomainTreeItem i = (DomainTreeItem) props.get("item");
			this.listener.onAddNode(i);
			String id = (String) props.get("domainId");
			DomainDetailContent ddc = new DomainDetailContent(constants,
					service, id,context);
			ddc.setContentChangeListener(this);
			panel.setWidget(ddc);
			return;
		}

		if (source.equals(DomainContentConstants.DOMAIN_DETAIL)
				&& dest.equals(DomainContentConstants.CREATE_BUS1)) {

			String dId = (String) props.get("domainId");
			CreateOrModifyBusContent1 cc = new CreateOrModifyBusContent1(
					constants, dId,true,null,null);
			cc.setContentChangeListener(this);
			panel.setWidget(cc);
			return;
		}
		if (source.equals(DomainContentConstants.CREATE_BUS1)
				&& dest.equals(DomainContentConstants.CREATE_BUS2)) {
			DomainTreeItem i = (DomainTreeItem) selectedItem;
			//String url = (String) props.get("url");
			//ServiceBus sc = (ServiceBus) props.get("bus");
			props.put("parentTreeId", i.getId());
			props.put("create", true);
			CreateOrModifyBusContent2 cc = new CreateOrModifyBusContent2(
					constants, service,props);
			cc.setContentChangeListener(this);
			panel.setWidget(cc);
			return;
		}
		if (source.equals(DomainContentConstants.CREATE_BUS2)
				&& dest.equals(DomainContentConstants.DOMAIN_DETAIL)) {
			DomainTreeItem i = (DomainTreeItem) props.get("item");
			this.listener.onAddNode(i);
			String id = (String) props.get("domainId");
			DomainDetailContent ddc = new DomainDetailContent(constants,
					service, id,context);
			ddc.setContentChangeListener(this);
			panel.setWidget(ddc);
			return;
		}
		if (source.equals(DomainContentConstants.CAT_DETAIL)
				&& dest.equals(DomainContentConstants.MODIFY_CAT1)) {

			String url = (String) props.get("url");
			ServiceCatalog sc = (ServiceCatalog) props.get("cat");
			CreateOrModifyCatalogContent1 ddc = new CreateOrModifyCatalogContent1(
					constants,  null, false, sc, url);
			ddc.setContentChangeListener(this);
			panel.setWidget(ddc);
			return;
		}
		
		if (source.equals(DomainContentConstants.MODIFY_CAT1)
				&& dest.equals(DomainContentConstants.MODIFY_CAT2)) {
			//String url = (String) props.get("url");
			//ServiceCatalog sc = (ServiceCatalog) props.get("cat");
			props.put("create", false);
			CreateOrModifyCatalogContent2 cc = new CreateOrModifyCatalogContent2(
					constants, service,props);
			cc.setContentChangeListener(this);
			panel.setWidget(cc);
			return;
		}
		if (source.equals(DomainContentConstants.MODIFY_CAT2)
				&& dest.equals(DomainContentConstants.CAT_DETAIL)) {
			DomainTreeItem i = (DomainTreeItem) selectedItem;
			String name = (String) props.get("name");
			i.setHTML(name);
			CatalogDetailContent ddc = new CatalogDetailContent(constants,
					service, i.getKey(),context);
			ddc.setContentChangeListener(this);
			panel.setWidget(ddc);
			return;
		}
		if (source.equals(DomainContentConstants.CAT_DETAIL)
				&& dest.equals(DomainContentConstants.REMOVE_CAT)) {

			
			ServiceCatalog sc = (ServiceCatalog) props.get("cat");
			RemoveCatalogContent ddc = new RemoveCatalogContent(
					constants,  service, sc);
			ddc.setContentChangeListener(this);
			panel.setWidget(ddc);
			return;
		}
		if (source.equals(DomainContentConstants.REMOVE_CAT)
				&& dest.equals(DomainContentConstants.DOMAIN_DETAIL)) {
			DomainTreeItem i = (DomainTreeItem) selectedItem.getParentItem();
			listener.onRemoveNode(selectedItem);
			DomainDetailContent ddc = new DomainDetailContent(constants,service,i.getKey(),context);
			ddc.setContentChangeListener(this);
			panel.setWidget(ddc);
			return;
		}
		if (source.equals(DomainContentConstants.REMOVE_CAT)
				&& dest.equals(DomainContentConstants.CAT_DETAIL)) {
			DomainTreeItem i = (DomainTreeItem) selectedItem;
			//listener.onRemoveNode(selectedItem);
			CatalogDetailContent ddc = new CatalogDetailContent(constants,service,i.getKey(),context);
			ddc.setContentChangeListener(this);
			panel.setWidget(ddc);
			return;
		}
		if (source.equals(DomainContentConstants.REMOVE_BUS)
				&& dest.equals(DomainContentConstants.BUS_DETAIL)) {
			DomainTreeItem i = (DomainTreeItem) selectedItem;
			//listener.onRemoveNode(selectedItem);
			BusDetailContent ddc = new BusDetailContent(constants,service,i.getKey(),context);
			ddc.setContentChangeListener(this);
			panel.setWidget(ddc);
			return;
		}
		if (source.equals(DomainContentConstants.REMOVE_BUS)
				&& dest.equals(DomainContentConstants.DOMAIN_DETAIL)) {
			DomainTreeItem i = (DomainTreeItem) selectedItem.getParentItem();
			listener.onRemoveNode(selectedItem);
			DomainDetailContent ddc = new DomainDetailContent(constants,service,i.getKey(),context);
			ddc.setContentChangeListener(this);
			panel.setWidget(ddc);
			return;
		}
		if (source.equals(DomainContentConstants.MODIFY_BUS1)
				&& dest.equals(DomainContentConstants.MODIFY_BUS2)) {
			DomainTreeItem i = (DomainTreeItem) selectedItem;
			//String url = (String) props.get("url");
			//ServiceBus sc = (ServiceBus) props.get("bus");
			props.put("parentTreeId", i.getId());
			props.put("create", false);
			CreateOrModifyBusContent2 cc = new CreateOrModifyBusContent2(
					constants, service,props);
			cc.setContentChangeListener(this);
			panel.setWidget(cc);
			return;
		}
		if (source.equals(DomainContentConstants.BUS_DETAIL)
				&& dest.equals(DomainContentConstants.MODIFY_BUS1)) {

			String url = (String) props.get("url");
			ServiceBus sc = (ServiceBus) props.get("bus");
			CreateOrModifyBusContent1 ddc = new CreateOrModifyBusContent1(
					constants,  null, false, sc, url);
			ddc.setContentChangeListener(this);
			panel.setWidget(ddc);
			return;
		}
		if (source.equals(DomainContentConstants.MODIFY_BUS2)
				&& dest.equals(DomainContentConstants.BUS_DETAIL)) {
			DomainTreeItem i = (DomainTreeItem) selectedItem;
			String name = (String) props.get("name");
			i.setHTML(name);
			BusDetailContent ddc = new BusDetailContent(constants,
					service, i.getKey(),context);
			ddc.setContentChangeListener(this);
			panel.setWidget(ddc);
			return;
		}
		if (source.equals(DomainContentConstants.BUS_DETAIL)
				&& dest.equals(DomainContentConstants.REMOVE_BUS)) {

			
			ServiceBus sc = (ServiceBus) props.get("bus");
			RemoveBusContent ddc = new RemoveBusContent(
					constants,  service, sc);
			ddc.setContentChangeListener(this);
			panel.setWidget(ddc);
			return;
		}

	}
}
